package org.example.controller;


import cn.hutool.json.JSONUtil;
import org.example.common.JWTHS256;
import org.example.common.Result;
import org.example.entity.ClassEntity;
import org.example.service.ClassService;
import org.example.service.UserService;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*",maxAge = 3600)
@RequestMapping("class")
public class ClassController {

    @Resource
    private ClassService classService;

    @Resource
    private UserService userService;

    /*
    添加课程
     */
    @RequestMapping(value = "add",method = RequestMethod.GET)
    public String addClass(ClassEntity newClass){
        Result result= new Result();

        try{
            int res = classService.addClass(newClass);
            int classid = newClass.getClassid();
            result.setStatus(200);
            result.setMsg("添加成功");
            Map<String,Object> data = new HashMap<>();
            data.put("classid",classid);
            result.setData(data);
        }catch (DataAccessException e){
            result.setStatus(205);
            result.setMsg("添加失败,课程已存在");
        }

        return JSONUtil.toJsonStr(result);
    }

    /*
    查找课程
     */

    @RequestMapping(value = "find",method = RequestMethod.GET)
    public String findClass(@RequestParam(value = "classid",required = false) String classid){
        Result result= new Result();
        List<ClassEntity> classList = classService.findClass(classid);

        if(classList.isEmpty()){
            result.setStatus(200);
            result.setMsg("查询结果为空");
            result.setData(classList);
        }else{
            result.setStatus(200);
            result.setMsg("查找成功");
            result.setData(classList);
        }

        return JSONUtil.toJsonStr(result);
    }

    /*
    选择课程
     */
    @RequestMapping(value = "get",method = RequestMethod.GET)
    public String chooseClass(@RequestParam(value = "schoolid",required = false) String schoolid,
                              @RequestParam(value = "classid",required = false) String classid){
        Result result= new Result();

        try{
            classService.chooseClass(schoolid,classid);
            result.setStatus(200);
            result.setMsg("选课成功");
        }catch (DataAccessException e){
            result.setStatus(403);
            result.setMsg("重复选课,选课失败");
        }

        return JSONUtil.toJsonStr(result);
    }

    /*
    查找用户的课程  老师/学生
     */
    @RequestMapping(value = "myclass",method = RequestMethod.GET)
    public String myclass(@RequestHeader("authorization") String token){
        String schoolid = JWTHS256.vaildToken(token);

        Result result = new Result();
        Map<String,Object> data = new HashMap<>();

        int role = userService.queryBySchoolid(schoolid).getRole();
        data.put("role",role);

        //如果是老师，查询老师的课
        if(role==0){
            List<Map<String,Object>> list = classService.queryTeacherClass(schoolid);
            data.put("list",list);
        }else {
            //如果是学生，查询学生选课
            List<Map<String,Object>> list = classService.queryStudentClass(schoolid);
            data.put("list",list);
        }
        result.setStatus(200);
        result.setMsg("查询成功");
        result.setData(data);

        return JSONUtil.toJsonStr(result);

    }

    /*
    课程详情
     */
    @RequestMapping(value = "details",method = RequestMethod.GET)
    public String classDetails(@RequestParam(value = "classid",required = false) String classid,
                               @RequestParam(value = "schoolid",required = false) String schoolid){

        Result result = new Result();
//        Map<String,Object> data = new HashMap<>();
//
//
//
//        int role = userService.queryBySchoolid(schoolid).getRole();
//        data.put("role",role);
//
//        List<Map<String,Object>> list = null;
//        data.put("list",list);

        result.setStatus(200);
        result.setMsg("接口还没写");
        //result.setData(data);

        return JSONUtil.toJsonStr(result);
    }

    /*
    学生申请成为该课程的审核
     */
    @RequestMapping(value="toaudit",method = RequestMethod.GET)
    public String toaudit(@RequestParam Map<String,String> params ){
        Result result = new Result();

        String classid = params.get("classid");
        String schoolid = params.get("schoolid");
        String invitecode = params.get("invitecode");

        ClassEntity classEntity = classService.queryClass(classid);

        //如果当前课程未开审核
        if(classEntity.getClassflag()==0){
            result.setStatus(206);
            result.setMsg("当前课程未开审核");
        }else {
            //当前课程开审核
            //邀请码正确
            if(invitecode.equals(classEntity.getInvitecode())){
                int res = classService.updateToAudit(classid,schoolid);
                if(res==1){
                    result.setStatus(200);
                    result.setMsg("更新成功");
                }else {
                    result.setStatus(207);
                    result.setMsg("无更新");
                }
            }else{
                //邀请码错误
                result.setStatus(207);
                result.setMsg("邀请码错误");
            }
        }

        return JSONUtil.toJsonStr(result);
    }


}
